package achievement.action;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import achievement.bean.Achievement;
import achievement.factory.AchievementFactory;
import achievement.service.AchievementService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AchievementAction extends ActionSupport implements SessionAware{
	private Achievement Achievement=new Achievement();
	private List<Achievement> Achievementlist=new ArrayList<Achievement>();
	private Map session;
	private File image;    //file to upload 
	private String imageFileName; //file name
	private String imageContentType;  //file type
	private InputStream inputStream;
	private String fileName;
	private int pagenow;
	
	public int getPagenow(){
		return pagenow;
	}
    public  void setPagenow(int pagenow){
    	this.pagenow=pagenow;
    }
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}
	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	 public File getImage() {
	        return image;
	    }

	    public void setImage(File img) {
	        this.image = img;
	    }

	    public String getImageFileName() {
	        return imageFileName;
	    }

	    public void setImageFileName(String imageFileName) {
	        this.imageFileName = imageFileName;
	    }

	    public String getImageContentType() {
	        return imageContentType;
	    }

	    public void setImageContentType(String imageContentType) {
	        this.imageContentType = imageContentType;
	    }




	public void setAchievement(Achievement Achievementbean){
		this.Achievement=Achievementbean;
	}
	
	public Achievement getAchievement(){
		return Achievement;
	}
	
	public List<Achievement> getAchievementlist(){
		return Achievementlist;
	}
	
	public void setAchievementlist(List<Achievement> Achievementlist){
		this.Achievementlist=Achievementlist;
	}
	
	public Map getSession() {
		return session;
	}

	@Override
	public void setSession(Map session) {
		this.session = session;
	}
	
	public String modifyAchievement() throws Exception {
		

		AchievementService Achievementservice=AchievementFactory.getInstance().creatAchievementService();
        if(!Achievementservice.ModifyAchievement(Achievement)){
        	this.addActionError("modifyAchievement error");
        	return INPUT;
        }
		
		Map session = ActionContext.getContext().getSession();
		session.remove("Achievement");
		return SUCCESS;
	}
	
	public String deleteAchievement() throws Exception {
		AchievementService Achievementservice =AchievementFactory.getInstance().creatAchievementService();
		if (!Achievementservice.DeleteAchievement(Achievement)) {
			this.addActionError("deleteAchievement error");
			return INPUT;
		} else {
			this.addActionError("deleteAchievement error");
			return SUCCESS;
		}
	}
	
	public String addAchievement() throws Exception {
		if(image!=null){
		Random rnd = new Random();
		int p = rnd.nextInt(20);
		String num = Integer.toString(p);
		String path = ServletActionContext.getServletContext().getRealPath(
				"/");
		path=path.replace("bestcleaner", "images"); 
		imageFileName = num + "." + imageFileName;
		File file = new File(path + "//" + imageFileName);
		imageFileName="/images/"+imageFileName;
		FileUtils.copyFile(image, file);
		}
		else 
			imageFileName="";
		AchievementService Achievementservice=AchievementFactory.getInstance().creatAchievementService();
		if (Achievement.getItem().trim().equalsIgnoreCase("")) {
			this.addActionError("addAchievement error");
			return INPUT;
		}
		if (Achievement.getProjectname().trim().equalsIgnoreCase("")) {
			this.addActionError("addAchievement error");
			return INPUT;
		}
		if(!Achievementservice.AddAchievement(Achievement)){
			this.addActionError("addAchievement error");
			return INPUT;
		}
		this.addActionError("addAchievement error");
		return SUCCESS;
	}

	 public String showallAchievement() throws Exception{
		 AchievementService Achievementservice=AchievementFactory.getInstance().creatAchievementService();
		 Achievementlist=Achievementservice.ShowAllAchievement(getPagenow(),6);
		 if(getPagenow()==0){
			 int total=Achievementservice.CountAllAchievement();
			 int page=0;
			 do {
					total = total - 6;
					page++;
				} while (total > 0);
				this.session.put("achievementtotalpage", page);
		 }
		 if(Achievementlist.size()==0){
			 this.addActionError("showallAchievement error");
			return INPUT;
		 }else{
			 return  SUCCESS; 
		 }
	 }
	 
	 public String showoneAchievement() throws Exception{
			AchievementService AchievementService =AchievementFactory.getInstance().creatAchievementService();
			Achievement= AchievementService.ShowoneAchievement(Achievement);
			if (Achievement == null) {
				this.addActionError("Error!");
				return INPUT;
			} else {
				this.session.put("Achievement", Achievement);
				return SUCCESS;
			}
		}
	 }

